/**
 * 
 */
package com.jm.web.builder;


import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.ParserException;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.utils.htmlNode.XmlNode;
import com.jm.web.form.Export;

/**
 * @author yanjie
 * Oct 31, 2011 8:41:47 AM
 */
public class ExportBuilder extends BaseBuilder{
	@Override
	public void RenderEnd() {
		if(this.getBaseTag().getPage().isExportFile()){
			try {
				if(!this.getBaseTag().getPage().isFastExport()){
					Parser parser = Utils.getParserInstance();
					AttributeBuilder attrs = new AttributeBuilder();
					attrs.InitFromHashtable(this.getBaseTag().getExtAttrList(),true);
					
					parser.setInputHTML(StringHelper.Format("<export %1$s >%2$s</export>",
							attrs.ToOutputString(),
							this.getBaseTag().getBodyContent().getString()));
					NodeIterator nodeIterator = parser.elements();  
			        if(nodeIterator.hasMoreNodes()) {
			           Node node = nodeIterator.nextNode();
			           Export export = new Export();
			           export.LoadConfig(XmlNode.getInsance(node,this.getBaseTag().getPage()));
			           export.init();
			           this.getBaseTag().getPage().addExportNode(export);
			        }
				}
			} catch (ParserException e) {
				e.printStackTrace();
			}
		}
	}
}
